package com.tjsinfo.tjsoa.baobiao.action;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.baobiao.bo.IHqBBYhManage;
import com.tjsinfo.tjsoa.baobiao.bo.IHqJshtdzManage;
import com.tjsinfo.tjsoa.baobiao.vo.THqBBYh;
import com.tjsinfo.tjsoa.baobiao.vo.THqJshtdzchy;
import com.tjsinfo.tjsoa.common.ToolKit;

public class JshtdzManageAction extends DispatchAction {
	
	private IHqBBYhManage hqBBYhManage;
	private IHqJshtdzManage hqJshtdzManage;

	public IHqBBYhManage getHqBBYhManage() {
		return hqBBYhManage;
	}

	public void setHqBBYhManage(IHqBBYhManage hqBBYhManage) {
		this.hqBBYhManage = hqBBYhManage;
	}	
	
	public IHqJshtdzManage getHqJshtdzManage() {
		return hqJshtdzManage;
	}

	public void setHqJshtdzManage(IHqJshtdzManage hqJshtdzManage) {
		this.hqJshtdzManage = hqJshtdzManage;
	}

	/**
	 * 获取推动组成员列表
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getList(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response)throws Exception {
			
			try {
				JSONArray array	 = new JSONArray();
				List<Map<String, String>> tempList = hqJshtdzManage.findAll();
	 			
		 		for (Map<String, String> tempMap: tempList ){
		 			JSONObject jsObject = new JSONObject();
		 			jsObject.put("id", tempMap.get("id"));
		 			jsObject.put("sort", tempList.indexOf(tempMap)+1);
		 			jsObject.put("dwmch", tempMap.get("dwmch"));
		 			jsObject.put("shj", tempMap.get("shj"));		
		 			jsObject.put("jl", tempMap.get("jl"));
		 			jsObject.put("zhgld", tempMap.get("zhgld"));	
		 			jsObject.put("jshgb", tempMap.get("jshgb"));	
		 			jsObject.put("qtchy", tempMap.get("qtchy"));	
		 			jsObject.put("xshdwsh", tempMap.get("xshdwsh"));	
		 			jsObject.put("jzhjshbmsh", tempMap.get("jzhjshbmsh"));	
		 			jsObject.put("tdzid", tempMap.get("tdzid"));
		 			array.put(jsObject);
		 		}
	 			response.getWriter().print(array);
	 		} catch (IOException e) {
	 			// TODO Auto-generated catch block
	 			e.printStackTrace();
	 		} 
			return null;
		}	
	
/**
	 * 准备进入添加记录界面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response)throws Exception {
			
		    int yhid = Integer.valueOf(request.getParameter("yhid"));
		    String dwmchString = hqBBYhManage.findByid(yhid).getDwmch();
		    THqJshtdzchy tdztemp = new THqJshtdzchy(0,yhid);
		    request.setAttribute("tdz", tdztemp);
		    request.setAttribute("dwmch", dwmchString);
		    request.setAttribute("type", "admin");
			return mapping.findForward("toUpdate");
	}	

	/**
	 * 管理员准备进入修改记录界面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response)throws Exception {
			
		    int id = Integer.valueOf(request.getParameter("id"));
		    THqJshtdzchy tdztemp = hqJshtdzManage.findByid(id);
		    String dwmchString = hqBBYhManage.findByid(tdztemp.getYhid()).getDwmch();
		    request.setAttribute("tdz", tdztemp);
		    request.setAttribute("dwmch", dwmchString);
		    request.setAttribute("type", "admin");
			return mapping.findForward("toUpdate");
	}	
	
	/**
	 * 普通用户准备进入修改记录界面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toUpdateByyh(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response)throws Exception {
			
		    int yhid = Integer.valueOf(request.getParameter("yhid"));
		    List <THqJshtdzchy> tdzList = hqJshtdzManage.findByyhid(yhid);
		    if (tdzList.size()<1){
		    	THqJshtdzchy tdztemp = new THqJshtdzchy(0,yhid);
		    	String dwmchString = hqBBYhManage.findByid(yhid).getDwmch();
			    request.setAttribute("tdz", tdztemp);
			    request.setAttribute("dwmch", dwmchString);
		    }else{
		    	THqJshtdzchy tdztemp = tdzList.get(0);
		    	String dwmchString = hqBBYhManage.findByid(tdztemp.getYhid()).getDwmch();
		    	request.setAttribute("tdz", tdztemp);
				request.setAttribute("dwmch",dwmchString );
		    }
		    request.setAttribute("type", "yh");
			return mapping.findForward("toUpdate");
	}	
	
	/**
	 * 保存记录
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward saveTdz(ActionMapping mapping, ActionForm form,
					HttpServletRequest request, HttpServletResponse response)throws Exception {
				
		THqJshtdzchy tdztemp=new THqJshtdzchy();
		
		tdztemp.setYhid(Integer.valueOf(request.getParameter("yhid")));
		tdztemp.setShj(request.getParameter("shj"));
		tdztemp.setJl(request.getParameter("jl"));
		tdztemp.setZhgld(request.getParameter("zhgld"));
		tdztemp.setJshgb(request.getParameter("jshgb"));
		tdztemp.setQtchy(request.getParameter("qtchy"));
		tdztemp.setXshdwsh(Integer.valueOf(request.getParameter("xshdwsh")));
		tdztemp.setJzhjshbmsh(Integer.valueOf(request.getParameter("jzhjshbmsh")));
		tdztemp.setRq(ToolKit.FormatDate(new Date()));
		String idString = request.getParameter("id");
		if ("0".equals(idString)){
			hqJshtdzManage.add(tdztemp);
		}else {
			tdztemp.setId(Integer.valueOf(idString));
			hqJshtdzManage.update(tdztemp);
		}
        String typeString = request.getParameter("type");
        if ("admin".equals(typeString)){
        	request.setAttribute("linkurl", "baobiao/jhshy/tdzlist.jsp");
        }else {
			request.setAttribute("linkurl", "tjsinfo/tjsoa/baobiao/JshTjbManage.do?method=getTjb");
		}
		request.setAttribute("message", "system.save.message");
		return mapping.findForward("success");
	}
	/**
	 * 准备进入添加用户界面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward enableYh(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response)throws Exception {
		    int yhid = Integer.valueOf(request.getParameter("id"));
		    THqBBYh hqYh = hqBBYhManage.findByid(yhid);
		    if ("0".equals(hqYh.getState())){
		    	hqYh.setState("1");
		    }else{
		    	hqYh.setState("0");
		    }
		    hqBBYhManage.update(hqYh);
		    JSONObject jsObject = new JSONObject();
		    jsObject.put("success",true);
		    response.getWriter().print(jsObject);
			return null;
		}		

}

